from datetime import datetime

from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_stock.entity.vo.account_vo import DeleteAccountModel, AccountModel, AccountPageQueryModel
from module_stock.service.account_service import AccountService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

accountController = APIRouter(prefix='/stock/account', dependencies=[Depends(LoginService.get_current_user)])


@accountController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:account:list'))]
)
async def get_stock_account_list(
        request: Request,
        account_page_query: AccountPageQueryModel = Depends(AccountPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    account_page_query_result = await AccountService.get_account_list_services(query_db, account_page_query,
                                                                               is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=account_page_query_result)


@accountController.post('', dependencies=[Depends(CheckUserInterfaceAuth('stock:account:add'))])
@ValidateFields(validate_model='add_account')
@Log(title='股票账号', business_type=BusinessType.INSERT)
async def add_stock_account(
        request: Request,
        add_account: AccountModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    # 检查账号是否已存在
    is_unique = await AccountService.check_account_no_unique_services(query_db, add_account.account_no)
    if not is_unique:
        return ResponseUtil.failure(msg=f'账号 {add_account.account_no} 已存在')

    add_account.create_by = current_user.user.user_name
    add_account.create_time = datetime.now()
    add_account.update_by = current_user.user.user_name
    add_account.update_time = datetime.now()
    add_account_result = await AccountService.add_account_services(query_db, add_account)
    logger.info(add_account_result.message)

    return ResponseUtil.success(msg=add_account_result.message)


@accountController.put('', dependencies=[Depends(CheckUserInterfaceAuth('stock:account:edit'))])
@ValidateFields(validate_model='edit_account')
@Log(title='股票账号', business_type=BusinessType.UPDATE)
async def edit_stock_account(
        request: Request,
        edit_account: AccountModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_account.update_by = current_user.user.user_name
    edit_account.update_time = datetime.now()
    edit_account_result = await AccountService.edit_account_services(query_db, edit_account)
    logger.info(edit_account_result.message)

    return ResponseUtil.success(msg=edit_account_result.message)


@accountController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('stock:account:remove'))])
@Log(title='股票账号', business_type=BusinessType.DELETE)
async def delete_stock_account(
        request: Request,
        ids: str,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    delete_account = DeleteAccountModel(ids=ids)
    delete_account.update_by = current_user.user.user_name
    delete_account.update_time = datetime.now()
    delete_account_result = await AccountService.delete_account_services(query_db, delete_account)
    logger.info(delete_account_result.message)

    return ResponseUtil.success(msg=delete_account_result.message)


@accountController.get(
    '/{id}', response_model=AccountModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:account:query'))]
)
async def query_detail_stock_account(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    account_detail_result = await AccountService.account_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=account_detail_result)
